let BASE_URL = "http://192.168.1.18:8080";
// let BASE_URL = "https://tch.blrwworkshop.com/prod-api";

const request = (obj, data, loading) => {
	if (loading) uni.showLoading({
		title: "加载中",
		mask: true
	});
	let Authorization = uni.getStorageSync("token") || ""
	let httpDefaultOpts = {
		url: BASE_URL + obj.url,
		data: data,
		beforeSend: function(xmlHttp) {
			xmlHttp.setRequestHeader("If-Modified-Since", "0");
			xmlHttp.setRequestHeader("Cache-Control", "no-cache");
		},
		method: obj.method || 'post',
		dataType: "json",
		header: {
			"Content-type": "application/json",
		},
	};
	if (Authorization) {
		if (httpDefaultOpts.header) {
			httpDefaultOpts.header = {
				Authorization: Authorization
			};
		} else {
			httpDefaultOpts.header["Authorization"] = Authorization;
		}
	} else {}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts)
			.then((res) => {
				// console.log("接口响应", res)
				if (loading) uni.hideLoading();
				if (res.data.code == 401) {
					uni.removeStorageSync('token')
					uni.showToast({
						title: '登录已失效',
						icon: 'none'
					})
					setTimeout(() => {
						uni.reLaunch({
							url: "/pages/login/loginType"
						})
					}, 1500)

					return
				} else {
					resolve(res.data);
				}

			})
			.catch((response) => {
				reject(response);
			});
	});
	return promise;
};
export default {
	BASE_URL,
	request,
};